package com.hy.study.reduce;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * 参数一, 参数二:reduce的输入是map的输出 参数三，参数四 最终输出的形式
 *
 * @author huyong
 * @date 2024 年 01 月 29 日 11:32: 12
 */
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    private final IntWritable intWritable = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        // 1.累计求和
        int sum = 0;
        for (IntWritable value : values) {
            sum += value.get();
        }
        // 2.写出
        intWritable.set(sum);
        context.write(key, intWritable);
    }
}
